package com.xqq.utils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class KmlToGeoJsonConverter {

    public static void main(String[] args) {
        String kmlDirectory = "F:\\我的星网\\项目相关\\缅甸\\标注数据\\更新内容汇总1204\\更新内容汇总\\全部\\标注点";  // KML 文件夹路径
        String geoJsonDirectory = "F:\\我的星网\\项目相关\\缅甸\\标注数据\\更新内容汇总1204\\更新内容汇总\\全部\\标注点\\geojson"; // 输出 GeoJSON 文件夹路径

        File folder = new File(kmlDirectory);
        File[] kmlFiles = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".kml"));

        if (kmlFiles != null) {
            for (File kmlFile : kmlFiles) {
                convertKmlToGeoJson(kmlFile, geoJsonDirectory);
            }
        }
    }

    private static void convertKmlToGeoJson(File kmlFile, String geoJsonDirectory) {
        try (BufferedReader br = new BufferedReader(new FileReader(kmlFile))) {
            StringBuilder kmlContent = new StringBuilder();
            String line;

            while ((line = br.readLine()) != null) {
                kmlContent.append(line);
            }

            JSONObject geoJson = new JSONObject();
            geoJson.put("type", "FeatureCollection");
            JSONArray features = new JSONArray();

            // 解析 KML 内容
            String[] placemarks = kmlContent.toString().split("<Placemark>");
            for (String placemark : placemarks) {
                if (placemark.contains("<name>") && placemark.contains("<Point>")) {
                    String name = placemark.substring(placemark.indexOf("<name>") + 6, placemark.indexOf("</name>"));
                    String coordinates = placemark.substring(placemark.indexOf("<coordinates>") + 13, placemark.indexOf("</coordinates>"));
                    String[] coords = coordinates.split(",");

                    JSONObject feature = new JSONObject();
                    feature.put("type", "Feature");

                    JSONObject properties = new JSONObject();
                    properties.put("name", name);
                    feature.put("properties", properties);

                    JSONObject geometry = new JSONObject();
                    geometry.put("type", "Point");
                    JSONArray coordArray = new JSONArray();
                    coordArray.put(Double.parseDouble(coords[0])); // 经度
                    coordArray.put(Double.parseDouble(coords[1])); // 纬度
                    geometry.put("coordinates", coordArray);
                    feature.put("geometry", geometry);

                    features.put(feature);
                }
            }

            geoJson.put("features", features);

            // 保存到 GeoJSON 文件
            String geoJsonFileName = geoJsonDirectory + "/" + kmlFile.getName().replace(".kml", ".geojson");
            try (FileWriter file = new FileWriter(geoJsonFileName)) {
                file.write(geoJson.toString(4)); // 4 是缩进级别
                System.out.println("Converted: " + kmlFile.getName() + " to " + geoJsonFileName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}